//
//  FQDDanmakuItemView.m
//  HJDanmakuDemo
//
//  Created by mewxiv on 9/16/16.
//  Copyright © 2016 olinone. All rights reserved.
//

#import "FQDDanmakuItemView.h"
#import "DanmakuBaseModel.h"
#import "FQDRestModel.h"
#import "UIImage+FQDExtension.h"

#import "UIImageView+WebCache.h"

static CGFloat const kInterItemSpacing = 5.0f;
static CGFloat const kAvatarDimension = 20.0f;
static CGFloat const kLabelTrailing = 10.0f;
static CGFloat const kItemBottomSpacing = 15.0f;

@interface FQDDanmakuItemView ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *avatarImageView;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIImageView *likeImageView;
@property (nonatomic, assign) CGSize labelSize;

@property (nonatomic, weak) DanmakuBaseModel *model;

@end

@implementation FQDDanmakuItemView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
    }
    return self;
}

- (void)prepareUI
{
    _bgView = [[UIView alloc] init];
    _bgView.backgroundColor = [UIColor colorWithWhite:1/3.0f alpha:2/3.0f];
    _bgView.layer.cornerRadius = 15.0f;
    [self addSubview:_bgView];
    
    CGFloat left = kInterItemSpacing;
    _avatarImageView = [[UIImageView alloc] init];
    _avatarImageView.frame = CGRectMake(left, kInterItemSpacing, kAvatarDimension, kAvatarDimension);
    [self addSubview:_avatarImageView];
    
    _contentLabel = [[UILabel alloc] init];
    [self addSubview:_contentLabel];
    
    UIImage *likeImage = [UIImage imageNamed:@"Liked_Mine"];
    _likeImageView = [[UIImageView alloc] initWithImage:likeImage];
    _likeImageView.hidden = YES;
    [self addSubview:_likeImageView];
}

- (void)configureWithModel:(DanmakuBaseModel *)model paintHeight:(CGFloat)paintHeight;
{
    self.model = model;
    
    FQDLabOpinion *opinion = model.opinion;
    self.contentLabel.font = [UIFont systemFontOfSize:model.textSize];
    self.contentLabel.text = opinion.content;
    self.contentLabel.textColor = model.textColor;
    
    NSURL *avatarURL = [NSURL URLWithString:opinion.author.avatar];
    [[SDWebImageManager sharedManager] downloadImageWithURL:avatarURL options:0 progress:nil
     completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
         UIImage *roundedImage = [image roundImage];
         self.avatarImageView.image = roundedImage;
    }];
    
    CGFloat left = CGRectGetMaxX(self.avatarImageView.frame) + kInterItemSpacing;
    CGSize labelSize = [self measureLabelSizeWithPaintHeight:paintHeight];
    self.contentLabel.frame = CGRectMake(left, 0, labelSize.width, paintHeight-kItemBottomSpacing);
    
    CGSize bgSize = [self measureSizeWithPaintHeight:paintHeight];
    bgSize.height -= kItemBottomSpacing;
    self.bgView.frame = (CGRect){ CGPointZero, bgSize };
}

- (CGSize)measureLabelSizeWithPaintHeight:(CGFloat)paintHeight
{
    NSString *text = self.model.opinion.content;
    CGFloat textSize = self.model.textSize;
    NSDictionary *attrs = @{ NSFontAttributeName : [UIFont systemFontOfSize:textSize] };
    self.labelSize = CGSizeMake([text sizeWithAttributes:attrs].width, paintHeight-kItemBottomSpacing);
    return self.labelSize;
}

- (CGSize)measureSizeWithPaintHeight:(CGFloat)paintHeight
{
    CGFloat width = kAvatarDimension + self.labelSize.width + kInterItemSpacing * 2 + kLabelTrailing;
    return CGSizeMake(width, paintHeight);
}

@end
